<?php
// ========================== 文件说明 ==========================//
// 本文件说明：友情链接管理
// ==============================================================//


if(!defined('IN_ROOT') || !isset($php_self) || !preg_match("/[\/\\\\]index\.php$/", $php_self)) {
	exit('Access Denied');
}

//权限检查
//permission();

// 检查链接名字是否符合逻辑
function checksitename($sitename) {
	if(!$sitename || strlen($sitename) > 30) {
		$result = '站点名不能空并不能大于30个字符<br />';
		return $result;
	}
	elseif(eregi("[<>{}(),%#|^&!`$]",$sitename)) {
		$result = '站点名中不能含有特殊字符<br />';
		return $result;
	}
}

// 检查链接描述是否符合逻辑
function checknote($note = '') {
	if($note && strlen($note) > 200) {
		$result = '站点描述不能大于200个字符<br />';
		return $result;
	}
}

//添加链接

if($_POST['action'] == 'addlink') {
	$link_title    = trim($_POST['title']);
	$link_url     = trim($_POST['url']);
	$link_content    = trim($_POST['content']);
	$state = intval($_POST['state']);

	$result  = checksitename($link_title);

	$result .= checkurl($link_url,0);

	$result .= checknote($note);

	if($result) {

		redirect($result);

	}
	$name    = char_cv($name);
	$url     = char_cv($url);
	$note    = char_cv($note);

	$rs = $db->fetch_one_array("SELECT count(*) AS links FROM {$tablepre}xna_links WHERE link_title='$link_title' AND link_url='$link_url'");
    if($rs['links'])	{

        redirect('该链接在数据库中已存在', 'index.php?job=link');

    }

    $db->query("INSERT INTO {$tablepre}xna_links (link_title, link_url, link_content, state) VALUES ('$link_title', '$link_url', '$link_content' ,'$state')");
	links_recache();

    redirect('添加链接成功', 'index.php?job=link');

}

//批量处理

if($_POST['action'] == 'domorelink') {
	if($ids = implode_ids($_POST['delete'])) {
		$db->query("DELETE FROM	{$tablepre}xna_links WHERE linkid IN ($ids)");
	}
	if(is_array($_POST['content'])) {
		foreach($_POST['content'] as $linkid => $value) {
			$db->query("UPDATE {$tablepre}xna_links SET displayorder='".intval($_POST['displayorder'][$linkid])."', link_title='".char_cv(trim($_POST['title'][$linkid]))."', link_url='".char_cv(trim($_POST['url'][$linkid]))."', link_content='".char_cv(trim($_POST['content'][$linkid]))."', state='".intval($_POST['state'][$linkid])."' WHERE linkid='".intval($linkid)."'");
		}
	}
	links_recache();

    redirect('链接已成功更新', 'index.php?job=link');

}


if(!$action) {
	$action = 'list';
}

if($action == 'add') {
	$subnav = '添加链接';
}

if ($action == 'list') {
	$query = $db->query("SELECT * FROM {$tablepre}xna_links ORDER BY displayorder");
	$linkdb = array();
	while ($link = $db->row($query)) {
		if ($link['state'] == '1') {
			$link['state'] = '<option value="1" selected>显示</option><option value="0">隐藏</option>';
		} else {
			$link['state'] = '<option value="1">显示</option><option value="0" selected>隐藏</option>';
		}
		$linkdb[] = $link;
	}
	unset($link);
	$db->free($query);
	$subnav = '编辑链接';
}

$navlink_L = ' &raquo; <a href="index.php?job=link">链接管理</a>'.($subnav ? ' &raquo; '.$subnav : '');
cpheader();
include template("link", _ROOT . "/template/admin/");

?>